package com.gaswell.config;


import com.gaswell.filter.JwtAuthenticationTokenFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

/**
 * @ClassName Securityconfig
 * @Author xiaow
 * @DATE 2022/9/15 9:55
 **/
@Configuration
//开启注解授权认证的注解
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {


    @Autowired
    private JwtAuthenticationTokenFilter authenticationTokenFilter;


    //    重写密码加密器
    @Bean
    public BCryptPasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }


    public static void main(String[] args) {
        String encode = new BCryptPasswordEncoder().encode("123");
        System.out.println(encode);
        encode = new BCryptPasswordEncoder().encode("123");
        System.out.println(encode);
        System.out.println(new BCryptPasswordEncoder().matches("123", encode));
    }

    @Bean
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }


    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
//               解决跨域问题
                .cors().and()
//                关闭csrf
                .csrf().disable()
//                禁用session
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                .and()
                .authorizeRequests()
//                放行接口 不需要认证
//                放行swagger          "/doc*","/chunk*","/app*"
                .antMatchers("/login", "/shoper/login", "/user/state", "/goods/getAllGoods"
                        , "/goods/getGoodsById", "/goods/getAllSeckillGoods", "/goods/getSeckillById", "/comments/getCommentsByGoodsId", "/goods/getAllByShopIdAll"
                        , "/shop/getShopInfoByShopId", "/user/streamQuery", "/user/streamQuery2", "/test/*", "/person/*", "/webjars/**", "/swagger-resources/**", "/*/api-docs/**",
                        "/swagger-ui.html", "/doc.html", "/login/login", "/qba01/outExcelMultiThread", "/qba01/outExcelSingleThread", "/qba01m/getByPropertiesDirect","/qba01/test"
                ).permitAll()
                .anyRequest().authenticated();

//    把我们自己写好的过滤器添加到过滤器的前面
        http.addFilterBefore(authenticationTokenFilter, UsernamePasswordAuthenticationFilter.class);


//        配置异常处理器
//        分别是认证异常处理和授权处理
//        http.exceptionHandling().authenticationEntryPoint(authenticationEntryPoint)
//                .accessDeniedHandler(accessDeniedHandler);
    }
}
